﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace Sbook.Data
{
    public class DeliveryController:SqlDataProvider
    {
        #region[Delivery_GetByAll]
        public List<DeliveryInfo> Delivery_GetByAll()
        {
            List<Data.DeliveryInfo> list = new List<Data.DeliveryInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Delivery_GetByAll", GetConnection()))
            {
                Data.DeliveryInfo obj = new Data.DeliveryInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.DeliveryIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion

        #region[Delivey_GetByTop]
        public List<Data.DeliveryInfo> Delivery_GetByTop(string top, string where, string order)
        {
            List<Data.DeliveryInfo> list = new List<Data.DeliveryInfo>();
            using (SqlCommand cmd = new SqlCommand("sp_Delivery_GetByTop", GetConnection()))
            {
                Data.DeliveryInfo obj = new DeliveryInfo();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@top",top);
                cmd.Parameters.AddWithValue("@where", where);
                cmd.Parameters.AddWithValue("@order", order);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.DeliveryIDataReader(dr));
                    }
                    dr.Close();
                }
            }
            return list;
        }
        #endregion

        #region[Delivey_GetCountByWhere]
        public int Delivey_GetCountByWhere(string where)
        {
            int count = 0;
            using (SqlCommand cmd = new SqlCommand("sp_Delivery_GetCountByWhere", GetConnection()))
            {
                Data.DeliveryInfo obj = new DeliveryInfo();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@where", where);
                //cmd.Parameters.AddWithValue("@order", order);

                count = (int)cmd.ExecuteScalar();

            }
            return count;
        }
        #endregion

        public bool Delivery_DeleteById(string Id)
        {
            using (SqlCommand cmd = new SqlCommand("sp_Delivery_DeleteById", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Id",Id);
                cmd.ExecuteNonQuery();
            }
            return true;
        }

        public bool Delivery_UpdateById(string set,string Id)
        {
            using (SqlCommand cmd = new SqlCommand("sp_Delivery_UpdateById", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@set",set);
                cmd.Parameters.AddWithValue("@Id", Id);
                cmd.ExecuteNonQuery();
            }
            return true;
        }

        #region[Delivery_Insert]
        public int Delivery_Insert(Data.DeliveryInfo obj)
        {
            int ID = int.MinValue;
            using (SqlCommand cmd = new SqlCommand("sp_Delivery_Insert", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ID",1));
                cmd.Parameters.Add(new SqlParameter("@DeliveryCode", obj.DeliveryCode));
                cmd.Parameters.Add(new SqlParameter("@DeliveryClass", obj.DeliveryClass));
                cmd.Parameters.Add(new SqlParameter("@DeliveryAdd", obj.DeliveryAdd));
                cmd.Parameters.Add(new SqlParameter("@DeliveryDate", obj.DeliveryDate));
                cmd.Parameters.Add(new SqlParameter("@DeliveryEmail", obj.DeliveryEmail));
                //cmd.Parameters.Add(new SqlParameter("@DeliveryLocation", obj.DeliveryLocation));
                cmd.Parameters.Add(new SqlParameter("@DeliveryName", obj.DeliveryName));
                cmd.Parameters.Add(new SqlParameter("@DeliveryPhone", obj.DeliveryPhone));
                cmd.Parameters.Add(new SqlParameter("@DeliveryPrice", obj.DeliveryPrice));
                cmd.Parameters.Add(new SqlParameter("@DeliveryShippingPoint", obj.DeliveryShippingPoint));
                cmd.Parameters.Add(new SqlParameter("@DeliveryStatus", obj.DeliveryStatus));
                cmd.Parameters.Add(new SqlParameter("@DeliveryUniversity", obj.DeliveryUniversity));
                cmd.Parameters.Add(new SqlParameter("@PaymentId", obj.PayId));
                cmd.Parameters.Add(new SqlParameter("@UId", obj.UId));
                cmd.Parameters.Add(new SqlParameter("@CustId", obj.CustId));
                cmd.Parameters.Add(new SqlParameter("@DeliveryApprovedDate",obj.DeliveryApprovedDate));
                cmd.Parameters[0].Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                ID=(int)cmd.Parameters[0].Value;

            }
            return ID;
        }
        #endregion

        #region[Delivery_Update]
        public bool Delivery_Update(Data.DeliveryInfo obj)
        {
            int ID = int.MinValue;
            using (SqlCommand cmd = new SqlCommand("sp_Delivery_Update", GetConnection()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@DeliveryId", obj.DeliveryId));
                cmd.Parameters.Add(new SqlParameter("@DeliveryCode", obj.DeliveryCode));
                cmd.Parameters.Add(new SqlParameter("@DeliveryDate", obj.DeliveryDate));
                cmd.Parameters.Add(new SqlParameter("@DeliveryShippingPoint", obj.DeliveryShippingPoint));
                cmd.Parameters.Add(new SqlParameter("@DeliveryStatus", obj.DeliveryStatus));
                cmd.Parameters.Add(new SqlParameter("@UId", obj.UId));
                cmd.Parameters.Add(new SqlParameter("@PaymentId", obj.PayId));
                cmd.Parameters.Add(new SqlParameter("@DeliveryName", obj.DeliveryName));
                cmd.Parameters.Add(new SqlParameter("@DeliveryAdd", obj.DeliveryAdd));
                cmd.Parameters.Add(new SqlParameter("@DeliveryPhone", obj.DeliveryPhone));
                cmd.Parameters.Add(new SqlParameter("@DeliveryEmail", obj.DeliveryEmail));
                cmd.Parameters.Add(new SqlParameter("@DeliveryClass", obj.DeliveryClass));
                cmd.Parameters.Add(new SqlParameter("@DeliveryUniversity", obj.DeliveryUniversity));
                cmd.Parameters.Add(new SqlParameter("@CustId", obj.CustId));
                cmd.Parameters.Add(new SqlParameter("@DeliveryApprovedDate", obj.DeliveryApprovedDate));
              
                cmd.ExecuteNonQuery();
               

            }
            return true;
        }
        #endregion

        #region[Delivery_GetByWherePaging]
        public List<DeliveryInfo> Delivery_GetByWherePaging(string CurentPage, string PageSize, string Where)
        {
            List<Data.DeliveryInfo> list = new List<Data.DeliveryInfo>();
            using (SqlCommand dbCmd = new SqlCommand("[sp_Delivery_GetByWherePaging]", GetConnection()))
            {
                Data.DeliveryInfo obj = new Data.DeliveryInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
                dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                dbCmd.Parameters.Add(new SqlParameter("@WherePaging", Where));
               
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.DeliveryIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        
    }
}